package com.cobla.struct.sort;

/**
 * @ClassName Sort
 * @Description 排序算法集合
 * @Author wt
 * @Date 2019/9/9
 * @Version 0.1
 */
public class Sort {

    /**
     * 直接插入排序
     * @param R
     */
    public static void insertSort(int R[]) {
        int mark;
        for (int i = 0; i < R.length; i++) {
            mark = i;
            for (int j = i+1; j < R.length; j++) {
                if (R[i] > R[j]) {
                    mark = j;
                }
            }
            if (i != mark) {
                int temp = R[mark];
                R[mark] = R[i];
                R[i] = temp;
            }
        }
    }

    /**
     * 折半
     * @param R
     */
    public static void halvedSort(int R[]){
        // TODO 先完成折半查找
    }

    /**
     * 希尔排序
     * @param R
     */
    public static void shellSort(int R[], int delta[]) {
        for (int i = 0; i < delta.length; i++) {
            shell(R, delta[i]);
        }
    }

    private static void shell(int R[], int delta){
        int mark = 0;
        for (int i = 0; i + delta < R.length; i++) {
            mark = i;
            for (int j = i + delta; j < R.length; j += delta) {
                if (R[mark] > R[j]) {
                    mark = j;
                }
            }
            if (mark != i) {
                int temp = R[mark];
                R[mark] = R[i];
                R[i] = temp;
            }
        }
    }
}
